module ObsObject.Star ( Star(..), genStars ) where

import qualified Data.IntMap as M

import Control.Monad  ( replicateM )
import Data.Binary    ( decodeFile )
import Random         ( randomRIO )
import FTable         ( mapWithArg, quad )
import Instrument.PSF ( SED,  PointSource(..) )

--

data Star = Star PointSource

-- Представление прямоугольного поля зрения инструмента (в градусах * 1e-4).

type Area = ((Double, Double), (Double, Double)) -- 

-- Время экспозиции

type Exposure = Double

-- Звездная величина (в фильтре V)

type Magnitude = Double

-- Произведение постоянной Планка на скорость  света [erg * А / sec^-1]

hc = 6.62606896E-27 * 2.99792458E+18

-- 

spTypes = ["uka0i.enc", "uka0iii.enc", "uka0iv.enc", "uka0v.enc", "uka2i.enc", "uka2v.enc", "uka3iii.enc", "uka3v.enc", "uka47iv.enc", "uka5iii.enc", "uka5v.enc", "uka7iii.enc", "uka7v.enc", "ukb0i.enc", "ukb0v.enc", "ukb12iii.enc", "ukb1i.enc", "ukb1v.enc", "ukb2ii.enc", "ukb2iv.enc", "ukb3i.enc", "ukb3iii.enc", "ukb57v.enc", "ukb5i.enc", "ukb5ii.enc", "ukb5iii.enc", "ukb6iv.enc", "ukb8i.enc", "ukb8v.enc", "ukb9iii.enc", "ukb9v.enc", "ukf02iv.enc", "ukf0i.enc", "ukf0ii.enc", "ukf0iii.enc", "ukf0v.enc", "ukf2ii.enc", "ukf2iii.enc", "ukf2v.enc", "ukf5i.enc", "ukf5iii.enc", "ukf5iv.enc", "ukf5v.enc", "ukf6v.enc", "ukf8i.enc", "ukf8iv.enc", "ukf8v.enc", "ukg0i.enc", "ukg0iii.enc", "ukg0iv.enc", "ukg0v.enc", "ukg2i.enc", "ukg2iv.enc", "ukg2v.enc", "ukg5i.enc", "ukg5ii.enc", "ukg5iii.enc", "ukg5iv.enc", "ukg5v.enc", "ukg8i.enc", "ukg8iii.enc", "ukg8iv.enc", "ukg8v.enc", "ukk01ii.enc", "ukk0iii.enc", "ukk0iv.enc", "ukk0v.enc", "ukk1iii.enc", "ukk1iv.enc", "ukk2i.enc", "ukk2iii.enc", "ukk2v.enc", "ukk34ii.enc", "ukk3i.enc", "ukk3iii.enc", "ukk3iv.enc", "ukk3v.enc", "ukk4i.enc", "ukk4iii.enc", "ukk4v.enc", "ukk5iii.enc", "ukk5v.enc", "ukk7v.enc", "ukm0iii.enc", "ukm0v.enc", "ukm10iii.enc", "ukm1iii.enc", "ukm1v.enc", "ukm2i.enc", "ukm2iii.enc", "ukm2p5v.enc", "ukm2v.enc", "ukm3ii.enc", "ukm3iii.enc", "ukm3v.enc", "ukm4iii.enc", "ukm4v.enc", "ukm5iii.enc", "ukm5v.enc", "ukm6iii.enc", "ukm6v.enc", "ukm7iii.enc", "ukm8iii.enc", "ukm9iii.enc", "uko5v.enc", "uko8iii.enc", "uko9v.enc", "ukrf6v.enc", "ukrf8v.enc", "ukrg0v.enc", "ukrg5iii.enc", "ukrg5v.enc", "ukrk0iii.enc", "ukrk0v.enc", "ukrk1iii.enc", "ukrk2iii.enc", "ukrk3iii.enc", "ukrk4iii.enc", "ukrk5iii.enc", "ukwf5v.enc", "ukwf8v.enc", "ukwg0v.enc", "ukwg5iii.enc", "ukwg5v.enc", "ukwg8iii.enc", "ukwk0iii.enc", "ukwk1iii.enc", "ukwk2iii.enc", "ukwk3iii.enc", "ukwk4iii.enc"]

sp = (t M.!) where
   t = M.fromList $ zip [1..n] spTypes
   n = length spTypes

--

genStars :: Int -> Area -> Exposure -> IO [Star]
genStars n a e =
  do
    replicateM n $ genStar a e

genStar :: Area -> Exposure -> IO Star
genStar ((x0,y0),(x1,y1)) e =
  do
    let n = length spTypes

    t <- randomRIO (   1, n   )
    m <- randomRIO (15.0, 23.0)
    x <- randomRIO (  x0, x1  )
    y <- randomRIO (  y0, y1  )

    f <- getFlux (sp t) m e

    print (sp t, m, x, y)

    return $ Star (PointSource f (x,y))

getFlux :: FilePath -> Double -> Exposure -> IO SED
getFlux fn m e =
  do
    f <- decodeFile   "db/filter/v.enc"
    v <- decodeFile   "db/vega.enc"
    o <- decodeFile $ "db/pickles/" ++ fn

    let a = quad $ o * f
    let b = quad $ v * f

    let c = 10**(-0.4*m) * b / a

    let k = fromRational $ toRational c

    let f = mapWithArg (\x v -> e * v * fromIntegral x / hc) (k * o)

    return f